const log4js = require('../../../../utils/logUtils');
const logger = log4js.getLogger('sip');

const xmlUtils = require("./xmlUtils");
const deviceCache = require("../../../../db/deviceCache");
const zlmedia = require("../../../../service/ZlMediaKitApi");
const sip = require("sip");
const TAG= "[BYE]";

module.exports={
    async handle(rq){
        //停止流
        const {pushKey, stream} = xmlUtils.toLocalPushKey(rq.headers);
        const target = deviceCache.pushArray[pushKey];
        logger.info(TAG, '收到关闭指令,key=', pushKey, '推流列表缓存', target);
        // 要立刻关闭，不然华为可能复用端口
        await zlmedia.stop(pushKey);
        if (target && target.playing) {
            delete deviceCache.pushArray[pushKey];
        }
        sip.send(sip.makeResponse(rq, 200, 'OK'));
    }
}
